use Test::More tests => 15;

use strict;
use Carp;
use Data::Dumper;

use DateTime::TimeZone;
use DateTime::Format::Strptime;
use DateTime::Cursor;

&test_new();
&test_clone();
&test_pivot_accessors();
&test_timezone_accessors();

sub test_clone {
  my $original = DateTime::Cursor->new();
  my $cloned = $original->clone();
  cmp_ok ($original, 'eq', $cloned, "original and clone are identical");
  return;
}

sub test_timezone_accessors {
  my $brit_tz = DateTime::TimeZone->new(name => 'Europe/London');
  my $brit_time = DateTime->now(time_zone => $brit_tz);
  my $yank_tz = DateTime::TimeZone->new(name => 'America/Denver');
  my $yank_time = DateTime->now(time_zone => $yank_tz);
  my $utc_tz = DateTime::TimeZone->new(name => 'UTC');

  # test changing the timezone
  my $cursor = DateTime::Cursor->new({pivot => $brit_time});
  my $got_tz = $cursor->get_timezone();
  ok(defined $got_tz, "tz from get_timezone() was defined");
  $cursor->set_timezone($utc_tz);
  cmp_ok($got_tz, "ne", $cursor->get_timezone(), "After set timezone from $brit_tz to UTC, new tz got is diff from old tz got");

  # test comparing diff cursors with diff timezones
  my $utc_cursor = DateTime::Cursor->new({pivot => $brit_time, timezone => $utc_tz});
  my $got_utc_tz = $utc_cursor->get_timezone();

  my $localized_cursor = DateTime::Cursor->new({pivot => $yank_time, timezone => $yank_tz}); 
  my $got_localized_tz = $localized_cursor->get_timezone();
  cmp_ok($got_tz, 'ne', $got_localized_tz, "tz from utc cursor does not equal tz from localized cursor"); 
  cmp_ok($utc_cursor, "==", $localized_cursor, "utc cursor == localized cursor");
  cmp_ok($utc_cursor, "ne", $localized_cursor, "utc cursor ne localized cursor");
  return;
}

sub test_new {
  eval {
      my %args = (pivot => "dummy value");
      my $bad_cursor = DateTime::Cursor->new(\%args);   
  };
  my $exception = $@;
  ok($exception =~ /^Pivot is not an instance of DateTime/o, "Raised exception, $exception, matched the expected result for using the with wrong value for constructor");

  ok(defined DateTime::Cursor->new(), "constructor with no args");
  ok(defined DateTime::Cursor->new({pivot => DateTime->now()}), "constructor with pivot arg");
  ok(defined DateTime::Cursor->new({pivot => DateTime->now(),
                                      interval => 'months'}), 
                                     "constructor with pivot, interval args");
  ok(defined DateTime::Cursor->new({pivot => DateTime->now(),
                                      timezone => DateTime::TimeZone->new(name => 'local')}),
                                     "constructor with pivot, timezone args");  
  ok(defined DateTime::Cursor->new({pivot => DateTime->now(),
                                      formatter => DateTime::Format::Strptime->new(pattern => '%T')}),
                                     "constructor with pivot, formatter args");
  return;
}

sub test_pivot_accessors {
  my $now = DateTime->now( time_zone => 'UTC' );
  my $cursor = DateTime::Cursor->new({pivot => $now});
  my $pivot = $cursor->get_pivot();
  ok(defined $pivot, "get_pivot returns a defined pivot");
  is($pivot, $now, "comparing original constructor pivot value with the one just gotten with the getter");
  my $another_cursor = DateTime::Cursor->new();
  ok(defined $another_cursor->get_pivot(), "Checking for defined pivot from cursor created with new without args");
  return;
}

1;
